package com.lc.rongzecaifu;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.rongzecaifu.activity.InvestmentActivity;
import com.lc.rongzecaifu.activity.ManageFinancesActivity;
import com.lc.rongzecaifu.conn.JsonInvest;
import com.lc.rongzecaifu.progress.SaundProgressBar;
import com.lc.rongzecaifu.rongzecaifu.R;

import java.text.NumberFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2/22/2016.
 */
public class MyLicailistAdapter_New2 extends RecyclerView.Adapter<MyLicailistAdapter_New2.ViewHolder> {

    private Context context;
    public final static int INVESTMODE = 1;//投资
    public final static int MANAGEFINANCESMODE = 2;//理财
    public final static int BONDMODE = 3;//债卷
    private int isInvest = 0;
    private List<JsonInvest.Info.ListContent> list;


    private SaundProgressBar mPbar;
    public static int progress = 0;
    private Message message;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            int p = msg.what;
            mPbar.setProgress(p);
        }

    };

    public MyLicailistAdapter_New2(Context context, List<JsonInvest.Info.ListContent> list, int isInvest) {
        this.context = context;
        this.list = list;
        this.isInvest = isInvest;
    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_rate)
        TextView tvRate;
        @Bind(R.id.tv_time)
        TextView tvTime;
        /*  @Bind(R.id.tv_danwei)
          TextView tvDanwei;*/
        /*@Bind(R.id.crpv)
        ColorfulRingProgressView crpv;*/
       /* @Bind(R.id.tv_progress)
        TextView tvProgress;*/
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sumMoney)
        TextView tvSumMoney;
        @Bind(R.id.regularprogressbar)
        SaundProgressBar mPbar;
        /*  @Bind(R.id.progress_horizontal)
          ProgressBar horizontalNumProgressBar;*/
       /* @Bind(R.id.text_danwei)
        TextView text_danwei;*/
        public String str;
        public int a;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
/*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_invest_financial, null)));*/
        View view = LayoutInflater.from(context).inflate(R.layout.list_invest_financial, null);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvName.setText(list.get(position).borrow_name);
        NumberFormat nfNumber = NumberFormat.getNumberInstance();
        holder.tvRate.setText("年化率  " + list.get(position).borrow_interest_rate + "%");
        holder.tvTime.setText("期限  " + list.get(position).borrow_duration + list.get(position).borrow_duration_cn);


        holder.mPbar.setMax(100);
        holder.tvSumMoney.setText(list.get(position).need);
        Drawable indicator = context.getResources().getDrawable(
                R.mipmap.jindu4);
        Rect bounds = new Rect(0, 0, indicator.getIntrinsicWidth(),
                indicator.getIntrinsicHeight());
        indicator.setBounds(bounds);

        holder.mPbar.setProgressIndicator(indicator);

        holder.mPbar.setVisibility(View.VISIBLE);


        //holder.mPbar.setProgress(59 + 1);
        String string = list.get(position).progress;
        holder.mPbar.setmText(string);

//        holder.mPbar.setProgress(Integer.parseInt(list.get(position).progress));
        String newStr = list.get(position).progress;
        Double db = Double.valueOf(newStr);
        int a = db.intValue();
//       holder.mPbar.setProgress(a);
        holder.mPbar.setProgress(a);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isInvest == INVESTMODE) {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).id);
                    intent.setClass(context, InvestmentActivity.class);
                    context.startActivity(intent);
                } else if (isInvest == 2) {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).id);
                    intent.setClass(context, ManageFinancesActivity.class);
                    context.startActivity(intent);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            message = handler.obtainMessage();
            // TODO Auto-generated method stub
            try {
                for (int i = 1; i <= 60; i++) {
                    int x = progress++;
                    message.what = x;
                    handler.sendEmptyMessage(message.what);
                    Thread.sleep(100);
                }

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

}
